package com.zx.tradeclient.ctrl.index6;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.Update;
import com.zx.base.widget.QuitDialog;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.entity.CompanyInfo;

public class Index6Activity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private RelativeLayout index6_update;
	private RelativeLayout index6_about;
	private RelativeLayout index6_Official_website;
	private RelativeLayout index6_copyright_information;
	private RelativeLayout index6_service_center;

	private TradeApp app;
	private ProgressDialog progressDialog;

	private CompanyInfo comInfo = new CompanyInfo();

	private int index = -1;
	private String mCopyRight = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index6);

		app = (TradeApp) getApplication();

		header_left = (Button) findViewById(R.id.header_left);
		header_left.setBackgroundResource(R.drawable.header_dquery);
		header_left.setVisibility(View.GONE);
		header_right = (Button) findViewById(R.id.header_right);

		index6_update = (RelativeLayout) findViewById(R.id.index6_update);
		index6_update.setOnClickListener(this);

		index6_about = (RelativeLayout) findViewById(R.id.index6_about);
		index6_about.setOnClickListener(this);

		index6_Official_website = (RelativeLayout) findViewById(R.id.index6_Official_website);
		index6_Official_website.setOnClickListener(this);

		index6_copyright_information = (RelativeLayout) findViewById(R.id.index6_copyright_information);
		index6_copyright_information.setOnClickListener(this);

		index6_service_center = (RelativeLayout) findViewById(R.id.index6_service_center);
		index6_service_center.setOnClickListener(this);

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (Constant.debug)
				Log.d("TMG", "ManageActivity KEYCODE_BACK");
			QuitDialog dialog = new QuitDialog(Index6Activity.this);
			dialog.exitConfirm();

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == index6_update) {

			Update up = new Update(Index6Activity.this, app, true);
			up.checkUpdate();

		} else if (v == index6_about) {

			if (comInfo.getCom_name().equals("")) {
				progressDialog = ProgressDialog.show(Index6Activity.this, null, "正在获取信息，请稍后.....", true, true);
				index = 1;
				getCompanyInfo();

			} else {
				Intent intent = new Intent(Index6Activity.this, AboutCompanyActivity.class);
				intent.putExtra("info", comInfo);
				startActivity(intent);
			}

		} else if (v == index6_Official_website) {
			if (comInfo.getCom_web().equals("")) {
				progressDialog = ProgressDialog.show(Index6Activity.this, null, "正在获取信息，请稍后.....", true, true);

				index = 2;

				getCompanyInfo();
			} else {
				String url = comInfo.getCom_web();
				if (url.indexOf("http") > 0) {

				} else {
					url = "http://" + url;
				}

				Uri uri = Uri.parse(url);
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(it);
			}

		} else if (v == index6_copyright_information) {

			progressDialog = ProgressDialog.show(Index6Activity.this, null, "正在获取信息，请稍后.....", true, true);
			getCopyRightInfo();

		} else if (v == index6_service_center) {

			if (comInfo.getCom_phone().equals("")) {
				progressDialog = ProgressDialog.show(Index6Activity.this, null, "正在获取信息，请稍后.....", true, true);
				index = 5;
				getCompanyInfo();

			} else {
				Intent intent = new Intent(Index6Activity.this, CustomerCenterActivity.class);
				intent.putExtra("phone", comInfo.getCom_phone());
				startActivity(intent);
			}

		}
	}

	private void getCompanyInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "aboutUs");

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(Index6Activity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {

		try {
			JSONObject object = response;
			String succeed = object.getString("succeed");

			if (succeed.equals("000")) {

				comInfo.setCom_name(object.getString("companyName"));
				comInfo.setCom_web(object.getString("webSite"));
				comInfo.setCom_email(object.getString("email"));
				comInfo.setCom_phone(object.getString("phoneNumber"));
				comInfo.setCom_fax(object.getString("fax"));
				comInfo.setCom_address(object.getString("address"));

				if (1 == index) {
					Intent intent = new Intent(Index6Activity.this, AboutCompanyActivity.class);
					intent.putExtra("info", comInfo);
					startActivity(intent);
				} else if (2 == index) {

					String url = comInfo.getCom_web();
					if (url.indexOf("http") > 0) {

					} else {
						url = "http://" + url;
					}

					Uri uri = Uri.parse(url);
					Intent it = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(it);
				} else if (5 == index) {
					Intent intent = new Intent(Index6Activity.this, CustomerCenterActivity.class);
					intent.putExtra("phone", comInfo.getCom_phone());
					startActivity(intent);
				}

			} else {
				Toast toast2 = Toast.makeText(Index6Activity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			progressDialog.dismiss();
			e.printStackTrace();
		}
	}

	private void getCopyRightInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "copyright");
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {

				// 解析数据

				try {
					JSONObject object = response;
					String succeed = object.getString("succeed");

					if (succeed.equals("000")) {

						mCopyRight = object.getString("content");

						Intent intent = new Intent(Index6Activity.this, CopyrightActivity.class);
						intent.putExtra("copyright", mCopyRight);
						startActivity(intent);

					} else {
						Toast toast2 = Toast.makeText(Index6Activity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}

					progressDialog.dismiss();

				} catch (JSONException e) {
					progressDialog.dismiss();
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(Index6Activity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

}